unit log_sync;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxClasses, cxControls, cxGridCustomView,
  cxGridDBTableView, cxGrid, ZAbstractRODataset, ZAbstractDataset, Setting,
  ZDataset, ZConnection, cxGridExportLink, ZAbstractTable;

type
  TlogSyncForm = class(TForm)
    Label1: TLabel;
    cbkabkot: TComboBox;
    cxGrid1: TcxGrid;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1TableView1: TcxGridTableView;
    cxGrid1Level1: TcxGridLevel;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    btnrefresh: TButton;
    DataSource1: TDataSource;
    btnexport: TButton;
    SaveDialog1: TSaveDialog;
    ZTable1: TZTable;
    procedure btnrefreshClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnexportClick(Sender: TObject);
    procedure cbkabkotChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; kode_login:string);overload;
  end;

var
  logSyncForm: TlogSyncForm;
  slogin:string;
  sKabkot:String;
  sKec:String;
  sDesa:String;
  skode:string;


implementation

{$R *.dfm}
constructor TlogSyncForm.Create(AOwner: TComponent; kode_login:string);
begin
  slogin:=kode_login;
  inherited Create(AOwner);
end;

procedure TlogSyncForm.FormCreate(Sender: TObject);
var
  setting:TSetting_Form;
begin
//  BorderIcons := BorderIcons - [biMaximize];
  ///showmessage(slogin);
  Setting := TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ZQuery1.Connection:=ZConnection1;
  ztable1.connection:=zconnection1;
 sKabkot:='';
  sKec:='';
  sDesa:='';
  skode:='';

  btnRefresh.Enabled:=false;
  btnexport.Enabled:=false;

  cbkabkot.Text:='';


  if(slogin <> '00') then
  begin
    cbkabkot.enabled:=false;
  end;

  with ZTable1 do begin
    Active:=False;
    TableName:='kabkot';
    cbKabkot.Clear;
    if (slogin='00') then begin
      Active:=True;
      while (not Eof) do begin
        cbkabkot.Items.Add(fieldbyname('TAMPIL').asstring);
        Next;
      end;
    end
    else
    begin
      Active:=True;
      filter:='KODE='+quotedstr(slogin);
      filtered:=true;
      cbkabkot.items.add(fieldbyname('TAMPIL').asstring);
      cbkabkot.itemindex:=0;
      sKabkot:=slogin;
      cbkabkot.Enabled:=false;
      btnrefresh.enabled:=true;
      btnexport.enabled:=true;


    end;
  end;
end;

procedure TlogSyncForm.btnrefreshClick(Sender: TObject);
begin
  cxGrid1DBTableView1.DataController.Filter.Clear;
  cxGrid1DBTableView1.ClearItems;
  with zquery1 do begin
      active:=false;
      sql.clear;
      sql.text:='SELECT a.USERNAME AS PELAKU, b.TAMPIL AS WILAYAH, a.TANGGAL AS WAKTU FROM `log_sinkronisasi` a LEFT JOIN `kecamatan` b ON a.ASAL LIKE b.KODE_KECAMATAN where b.KODE_KABKOT='+quotedstr(skabkot);
      active:=true;
   end;
   DataSource1.DataSet:=zquery1;
   cxGrid1DBTableView1.DataController.DataSource:=DataSource1;
   cxGrid1DBTableView1.DataController.CreateAllItems;
end;

procedure TlogSyncForm.btnexportClick(Sender: TObject);
begin
  if SaveDialog1.Execute then
    ExportGridToExcel(saveDialog1.FileName,cxGrid1,False);
end;

procedure TlogSyncForm.cbkabkotChange(Sender: TObject);
var
  pilihan:String;
begin
  if (cbKabkot.ItemIndex > -1) then
  begin
    pilihan := cbKabkot.Items[cbKabkot.ItemIndex];
    with ZTable1 do
      begin
        Active := False;
        TableName := 'kabkot';
        Filter := 'TAMPIL = '+QuotedStr(pilihan);
        Filtered := True;
        Active := True;
        First;
        sKabkot := FieldByName('KODE').AsString;
        //cb2
        Active := False;
        TableName := 'kecamatan';
        Filter := 'KODE_KABKOT = '+QuotedStr(sKabkot);
        Filtered := True;
        Active := True;
        First;
        btnRefresh.Enabled:=true;
        btnexport.Enabled:=true;

      end;
  end
    else
  begin
    sKabkot:='';
    btnrefresh.enabled:=false;
      btnexport.enabled:=false;

  end;
  sKec:='';
  sDesa:='';

end;

procedure TlogSyncForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
action:=caFree;
end;

end.
